#pragma execution_character_set("utf-8")
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTextCodec>
#include "iconhelper.h"
#include <QFile>
#define SIP ("192.168.1.230" )
#define PORT (8888)

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    qDebug() <<"MainWindow(QWidget *parent)" << endl;
    ui->setupUi(this);
    this->initForm();
    this->initsock();
    setWindowTitle(QString::fromLocal8Bit("客户端"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

}

MainWindow::~MainWindow()
{
    qDebug() <<"~MainWindow()" << endl;
    delete ui;
}

/*初始化套接字*/
void MainWindow::initsock()
{
    qDebug() <<"initsock()" << endl;
    tcpSocket = new QTcpSocket;
    user = new Usr();
    newTcpConnect();
    slot_connecct();
    connect(tcpSocket,SIGNAL(readyRead()),SLOT(Slot_recvInfo()));
}

/*接受数据*/
void MainWindow::Slot_recvInfo()
{
    /* 读取所有数据 */
    qDebug() <<"Slot_recvInfo()" << endl;
      QString str = this->tcpSocket->readAll();
      qDebug() << str << endl;

      /* 截取字符串 */
      QStringList list = str.split(":");

      /* 获取命令 */
      QString cmd = list.at(0);
      qDebug() << "命令:" << cmd << endl;

      /* 获取数据 */
      QString data = list.at(1);
      qDebug() << "数据:" << data << endl;

      qDebug() << "测试switch1" << endl;
      u_long num = cmd.toInt();
      qDebug() <<  num << endl;
      switch (num) {
      /* 登录 */
        case LOGIN_OK:
        {
            qDebug() << "登录" << endl;
            this->isLogin = true;
            ui->stackedWidget->setCurrentIndex(userPage);

        }break;
        case PASSWORD_ERR:{
            ui->stackedWidget->setCurrentIndex(logPage);
        }break;
         case NOT_FOUNT_USR:{
            ui->stackedWidget->setCurrentIndex(regPage);
         }break;
        case JOIN_OK:{
          this->isLogin = true;
            ui->stackedWidget->setCurrentIndex(userPage);
        }break;
        case RESERVATION_OK:{

            ui->resplacenum->setText(data);
            ui->stackedWidget->setCurrentIndex(resPage);
        }break;
        case PAY_OK:{
            QString buf = "please pay "+ data + " yuan";
            QMessageBox::information(NULL, "Pay", buf.toUtf8());
            //sendInfo(PICK_CAR_OK," ");
        }break;
         case SHOW:{
            qDebug() <<"SHOW()" << endl;
             ui->shownum->append(data);
         }break;
         case PICK_CAR_OK:{
            sendInfo(PAY," ");
          }break;
      }

}

/*点击进入app初始页面*/
void MainWindow::slot_connecct()
{
     qDebug() <<"slot_connecct()" << endl;
    QFile file("./usr.txt");
    file.open(QFile::ReadWrite);
    QString buf = file.readAll();
    if(!buf.isEmpty()){
        QStringList s = buf.split(" ");
        user->setName(s.at(0));
        user->setPhoneNum(s.at(1));
        user->setPassword(s.at(2));
        QString buf = user->getPhoneNum() + " " + user->getPassword() ;
        sendInfo(LOGIN,buf);
        file.close();

        this->isLogin = true;

        ui->showname->setText(user->getName());
        ui->showphnum->setText(user->getPhoneNum());

        ui->stackedWidget->setCurrentIndex(userPage);
    }else{
        ui->stackedWidget->setCurrentIndex(logPage);
    }


}


/*初始化套接字*/
void MainWindow::newTcpConnect()
{
    qDebug() <<"newTcpConnect()" << endl;
    tcpSocket->abort();
    tcpSocket->connectToHost(SIP,PORT);
}

/*初始化app界面*/
void MainWindow::initForm()
{
     qDebug() <<"initForm()" << endl;
    //setWindowTitle("手机APP主界面");

    this->setProperty("form", true);
    this->setProperty("canMove", true);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);

    //顶部标题栏加粗显示
    int fontSize = this->font().pointSize() + 3;
    IconHelper::Instance()->setIcon(ui->labMenu, QChar(0xf036), fontSize);
    IconHelper::Instance()->setIcon(ui->labAdd, QChar(0xf067), fontSize);
    ui->labMenu->setFixedWidth(ui->labMenu->sizeHint().width() + 5);
    ui->labAdd->setFixedWidth(ui->labAdd->sizeHint().width() + 8);

    ui->labTitle->setFont(QFont(this->font().family(), fontSize - 1));
    ui->labTitle->setText("勇哥的停车场");
    ui->widgetNav->setProperty("flag", "bottom");

    //设置按钮的固定高度
    QStringList qss;
    qss.append(QString("QWidget#widgetNav QAbstractButton{min-height:%1px;max-height:%1px;}").arg(50));
    this->setStyleSheet(qss.join(""));

    pixChar << 0xf042 << 0xf1a4 << 0xf002 << 0xf007;
    btns << ui->tbtn1 << ui->tbtn2 << ui->tbtn3 << ui->tbtn4;

    int borderWidth = 3;
    QString borderColor = "#029FEA";
    QString normalBgColor = "#292F38";
    QString selectBgColor = "#1D2025";
    QString normalTextColor = "#54626F";
    QString selectTextColor = "#FDFDFD";

    IconHelper::Instance()->setStyle(ui->widgetNav, btns, pixChar, 15, 20, 20, "bottom",
                                     borderWidth, borderColor, normalBgColor, selectBgColor, normalTextColor, selectTextColor);

    //设置对应的按钮的图标显示方案以及是否可以选中以及对应的单击事件
    int count = btns.count();
    for (int i = 0; i < count; i++) {
        btns.at(i)->setCheckable(true);
        btns.at(i)->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        connect(btns.at(i), SIGNAL(clicked(bool)), this, SLOT(buttonClick()));
    }

    ui->tbtn1->click();
}

/*当按钮按下时*/
void MainWindow::buttonClick()
{
    qDebug() <<"buttonClick()" << endl;
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();

    int index = 0;
    int count = btns.count();
    for (int i = 0; i < count; i++) {
        if (btns.at(i) == b) {
            index = i;
            btns.at(i)->setChecked(true);
            btns.at(i)->setIcon(QIcon(IconHelper::Instance()->getPixmap(btns.at(i), false)));
        } else {
            btns.at(i)->setChecked(false);
            btns.at(i)->setIcon(QIcon(IconHelper::Instance()->getPixmap(btns.at(i), true)));
        }
    }

    qDebug() << "单击了" << name;
    ui->stackedWidget->setCurrentIndex(index);

    if (name == "我的") {
        //close();
    }
}

/*发送注册信息*/
void MainWindow::on_regok_clicked()
{
     qDebug() <<"on_regok_clicked()" << endl;
    user->setName(ui->regname->text());

    user->setPassword(ui->regpwd->text());

    user->setPhoneNum(ui->regphnum->text());

    if(!user->getName().isEmpty() && !user->getPassword().isEmpty() && !user->getPhoneNum().isEmpty())
    {

        QFile file("./usr.txt");
        file.open(QFile::ReadWrite);
        QString buf =  user->getName()+" "+user->getPhoneNum()+" "+user->getPassword();

        file.write(buf.toUtf8());

        ui->showname->setText(user->getName());
        ui->showphnum->setText(user->getPhoneNum());

        ui->stackedWidget->setCurrentIndex(userPage);
         file.close();
         sendInfo(JOIN,buf);
    }

}

/*预约发送车牌号*/
void MainWindow::on_pushButton_clicked()
{
    if(this->isLogin)
    {
        qDebug() <<"on_pushButton_clicked()" << endl;
        user->setPlateNumbe(ui->rescarnum->text());
        if(!user->getPlateNumbe().isEmpty()){
            sendInfo(RESERVATION,user->getPlateNumbe());
        }
    }
    else{
        ui->stackedWidget->setCurrentIndex(logPage);
    }
}

/*发送数据*/
void MainWindow::sendInfo(int cmd, const QString &data)
{
    qDebug() <<"sendInfo(int cmd, const QString &data)" << endl;
    QString str = QString::number(cmd);
    str += ":";
    str += data;
    this->tcpSocket->write(str.toUtf8());
}

/*发送登录信息*/
void MainWindow::on_logbtn_clicked()
{
    qDebug() <<"on_logbtn_clicked()" << endl;
    user->setPhoneNum(ui->logphnum->text());
    user->setPassword(ui->logpwd->text());
    if(!user->getPhoneNum().isEmpty() && !user->getPassword().isEmpty())
    {
        QString buf =  user->getPhoneNum() + " " + user->getPassword();

        ui->showphnum->setText(user->getPhoneNum());

        ui->stackedWidget->setCurrentIndex(userPage);
        sendInfo(JOIN,buf);
    }
}




//发送取车信息
void MainWindow::on_getcar_clicked()//取车
{
    qDebug() <<"on_getcar_clicked()" << endl;
    sendInfo(PICK_CAR, user->getPlateNumbe());
}

void MainWindow::on_regbtn_clicked()//注册
{
    qDebug() <<"on_regbtn_clicked()" << endl;
    ui->stackedWidget->setCurrentIndex(regPage);
}

void MainWindow::on_logoutbtn_clicked()//退出
{
    qDebug() <<"on_logoutbtn_clicked()" << endl;
    this->close();
}

void MainWindow::on_delbtn_clicked()//注销
{
    qDebug() <<"on_delbtn_clicked()" << endl;
    this->isLogin = false;
    ui->stackedWidget->setCurrentIndex(logPage);
}
